// address-new.js
var app = getApp();
var iFDU = require('../../utils/iFDU.js');
Page({
  data: {
    config: app.config,
    globalData: app.globalData,
    addressId: 0,
    receiver: '',
    mobile: '',
    provinceIndex: -1,
    provinceId: 0,
    provinces: [],
    cityIndex: -1,
    cityId: 0,
    cities: [],
    areaIndex: -1,
    areaId: 0,
    areas: [],
    needCb: 0,
  },

  onLoad: function (options) {
    var that = this;
    var addressId = options.addressId;

    if (options.needCb) {
      this.setData({
        needCb: options.needCb
      });
    }

    app.getUserInfo(function (globalData) {
      that.setData({
        globalData: globalData
      });

      if (addressId) {
        iFDU.api.doGet(
          'address/getAddressById',
          {
            userId: globalData.userId,
            id: addressId,
          },
          function (address) {
            that.loadAreaList(1, 0, function (data) {
              var len = data.length;
              var provinceIndex = -1;
              for (var i = 0; i < len; i++) {
                if (data[i].id == address.address.province_id) {
                  provinceIndex = i;
                  break;
                }
              }

              console.log('provinceIndex', provinceIndex);

              that.setData({
                provinces: data,
                provinceIndex: provinceIndex
              })
            });

            that.loadAreaList(2, address.address.province_id, function (data) {
              var len = data.length;
              var cityIndex = -1;
              for (var i = 0; i < len; i++) {
                if (data[i].id == address.address.city_id) {
                  cityIndex = i;
                  break;
                }
              }

              console.log('cityIndex', cityIndex);

              that.setData({
                cities: data,
                cityIndex: cityIndex
              })
            });

            that.loadAreaList(3, address.address.city_id, function (data) {
              var len = data.length;
              var areaIndex = -1;
              for (var i = 0; i < len; i++) {
                if (data[i].id == address.address.area_id) {
                  areaIndex = i;
                  break;
                }
              }

              console.log('areaIndex', areaIndex);

              that.setData({
                areas: data,
                areaIndex: areaIndex
              })
            });

            that.setData({
              addressId: addressId,
              receiver: address.address.receiver,
              mobile: address.address.phone,
              provinceId: address.address.province_id,
              cityId: address.address.city_id,
              areaId: address.address.area_id,
              address: address.address.address,
            });
          }
        )
      } else {
        that.loadAreaList(1, 0, function (res) {
          that.setData({
            provinces: res,
            cities: [],
            areas: [],
          })
        });
      }
    });
  },

  loadAreaList: function (type, parentId, cb) {
    var that = this;
    iFDU.api.doGet(
      'address/getAreaList',
      {
        userId: that.data.globalData.userId,
        type: type,
        parentId: parentId,
        rnd: Math.random()
      },
      function (data) {
        cb(data);
      }
    )
  },

  bindReceiverChange: function (e) {
    var val = e.detail.value;
    this.setData({
      receiver: val
    });
  },

  bindMobileChange: function (e) {
    var val = e.detail.value;
    this.setData({
      mobile: val
    });
  },

  bindProvinceChange: function (e) {
    var that = this;

    that.loadAreaList(2, that.data.provinces[e.detail.value].id, function (res) {
      that.setData({
        provinceIndex: e.detail.value,
        provinceId: that.data.provinces[e.detail.value].id,
        cityIndex: -1,
        cities: res,
        areaIndex: -1,
        areas: [],
      })
    });
  },

  bindCityChange: function (e) {
    var that = this;

    that.loadAreaList(3, that.data.cities[e.detail.value].id, function (res) {
      that.setData({
        cityIndex: e.detail.value,
        cityId: that.data.cities[e.detail.value].id,
        areaIndex: -1,
        areas: res,
      })
    });
  },

  bindAreaChange: function (e) {
    var that = this;
    this.setData({
      areaIndex: e.detail.value,
      areaId: that.data.areas[e.detail.value].id,
    })
  },

  bindAddressChange: function (e) {
    var val = e.detail.value;
    this.setData({
      address: val
    });
  },

  addNewAddress: function () {
    var that = this;
    if (!this.data.receiver) {
      iFDU.toast.showToast({
        title: '请输入收件人',
        mask: false
      });
      return;
    }
    if (this.data.receiver.length > 16) {
      iFDU.toast.showToast({
        title: '收件人最多输入16个字',
        mask: false
      });
      return;
    }
    if (!this.data.mobile) {
      iFDU.toast.showToast({
        title: '请输入手机号码',
        mask: false
      });
      return;
    }
    if (!/^1[0-9]{10}$/.test(this.data.mobile)) {
      iFDU.toast.showToast({
        title: '手机号码格式不正确',
        mask: false
      });
      return;
    }
    if (this.data.provinceIndex < 0) {
      iFDU.toast.showToast({
        title: '请选择省份',
        mask: false
      });
      return;
    }

    if (this.data.cityIndex < 0) {
      iFDU.toast.showToast({
        title: '请选择城市',
        mask: false
      });
      return;
    }

    if (this.data.areaIndex < 0) {
      iFDU.toast.showToast({
        title: '请选择区域',
        mask: false
      });
      return;
    }

    if (!this.data.address) {
      iFDU.toast.showToast({
        title: '请输入详细地址',
        mask: false
      });
      return;
    }
    if (this.data.address.length > 50) {
      iFDU.toast.showToast({
        title: '地址最多输入50个字',
        mask: false
      });
      return;
    }
    if (that.data.addressId) {
      iFDU.api.doGet(
        'address/updateAddress',
        {
          userId: that.data.globalData.userId,
          id: that.data.addressId,
          receiver: that.data.receiver,
          phone: that.data.mobile,
          provinceId: that.data.provinces[that.data.provinceIndex].id,
          cityId: that.data.cities[that.data.cityIndex].id,
          areaId: that.data.areas[that.data.areaIndex].id,
          address: that.data.address,
          rnd: Math.random()
        },
        function (data) {
          wx.navigateBack({
          });
        }
      )
    } else {
      iFDU.api.doGet(
        'address/addAddress',
        {
          userId: that.data.globalData.userId,
          receiver: that.data.receiver,
          phone: that.data.mobile,
          provinceId: that.data.provinces[that.data.provinceIndex].id,
          cityId: that.data.cities[that.data.cityIndex].id,
          areaId: that.data.areas[that.data.areaIndex].id,
          address: that.data.address,
          rnd: Math.random()
        },
        function (data) {
          if (!that.data.needCb || that.data.needCb == 0) {
            wx.navigateBack({
            });
          } else {
            iFDU.api.doGet(
              'address/getAddressById',
              {
                userId: that.data.globalData.userId,
                id: data.id
              },
              function (data1) {
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2];
                prevPage.setData({
                  addressId: data.id,
                  address: data1.address
                })
                wx.navigateBack({
                });
              }
            )
          }
        }
      )
    }
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '爱上衬衫，从「逸见」开始…',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})